import {
  getComplaintLevelList, //投诉等级列表
  getComplaintChannelList,
} from "@/api/workorder"
import {
  getOptionTree,
} from "@/api/system/dict";

/**
 * 缓存接口数据
 */
 const cahceData = {
  complaintLevelList: {
    data: null,
    requestFunction: () => {
      return getComplaintLevelList().then(res => {
        if (res.data.data && res.data.data.length) {
          return res.data.data
        } else {
          return null
        }
      })
    }
  },
  complaintChannelList: {
    data: null,
    requestFunction: () => {
      return getComplaintChannelList().then(res => {
        if (res.data.data && res.data.data.length) {
          return res.data.data
        } else {
          return null
        }
      })
    }
  },
  consultHappyServiceTypeList: {
    data: null,
    requestFunction: () => {
      return _optionTreeGenerate({ code: 'transport_service_type' })
    }
  },
  'tying_sale_type': {
    data: null,
    requestFunction: () => {
      return _optionTreeGenerate({ code: 'tying_sale_type' })
    }
  },
  'ask_service_type': {
    data: null,
    requestFunction: () => {
      return _optionTreeGenerate({ code: 'ask_service_type' })
    }
  },
  'refund_reason': {
    data: null,
    requestFunction: () => {
      return _optionTreeGenerate({ code: 'refund_reason' })
    }
  },
}

// 字典树方法
function _optionTreeGenerate(options) {
  return getOptionTree(options).then(res => {
    if (res.data.data && res.data.data.length) {
      return res.data.data
    } else {
      return null
    }
  })
}

export const getCacheData = async (key) => {
  const dataObj = cahceData[key]
  if (dataObj.data) {
    return dataObj.data
  } else {
    const resData = await dataObj.requestFunction()
    dataObj.data = resData
    return dataObj.data
  }
}